package com.execute;

import com.common.protobuf.MessageModule;
import com.scanner.Invoker;
import com.scanner.InvokerTable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;

/**
 * @author 顾志杰
 * @date 2021/3/16-14:04
 */
public class MessageTask4Response implements Runnable{

    private MessageModule.Message message;

    private ChannelHandlerContext ctx;

    public MessageTask4Response(MessageModule.Message message, ChannelHandlerContext ctx){
        this.message = message;
        this.ctx = ctx;
    }

    @Override
    public void run() {

        try {
            // user-return
            String module = this.message.getModule();
            // save-return
            String cmd = this.message.getCmd();
            // 响应的结果
            MessageModule.ResultType resultType = this.message.getResultType();
            // 响应的内容
            byte[] data = this.message.getBody().toByteArray();

            Invoker invoker = InvokerTable.getInvoker(module, cmd);
            assert invoker != null;
            invoker.invoke(resultType,data);
        } finally {
            // 资源释放
            ReferenceCountUtil.release(message);
        }

    }
}
